package ui.button
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BrightnessButtonView extends Sprite
	{
		private const DESATURATE_MATRIX:Array = [0.3086000084877014,0.6093999743461609,0.0820000022649765,
												 0,0,0.3086000084877014,0.6093999743461609,
												 0.0820000022649765,0,0,0.3086000084877014,
												 0.6093999743461609,0.0820000022649765,0,0,0,0,0,1,0]
		
		private var brightFliter:ColorMatrixFilter;
		private var bwFilter:ColorMatrixFilter;
		private var allFilters:Array;
		private var _desaturate:Boolean;
		
		private const CELL_SIZE:int = 10;
		
		public function BrightnessButtonView()
		{
			brightFliter = new ColorMatrixFilter([1,0,0,0,-40,0,1,0,0,-40,0,0,1,0,-40,0,0,0,1,0]);
			bwFilter = new ColorMatrixFilter(DESATURATE_MATRIX);
			
			allFilters = new Array;	
			this.buttonMode = true;
			
			if(numChildren > 0 && getChildAt(0) is Shape)
				clipTranspanretContent();
			
			addEventListener(MouseEvent.MOUSE_DOWN,changeButtonState);
			addEventListener(MouseEvent.MOUSE_UP,changeButtonState);
			addEventListener(MouseEvent.ROLL_OUT,changeButtonState);
			
		}
		
		public function clipTranspanretContent():void
		{
			var rect:Rectangle = this.getBounds(this);
			if(rect.width < 1 || rect.height < 1)
				return;
			
			var bpData:BitmapData = new BitmapData(rect.right,rect.bottom,true,0x00000000);
			bpData.draw(this);
			
			while(this.numChildren > 0)
				this.removeChildAt(0);
			
			var hCellCount:Number = Math.ceil(bpData.height / CELL_SIZE);
			var vCellCount:Number = Math.ceil(bpData.width / CELL_SIZE);
			
			graphics.beginBitmapFill(bpData,null,false,true);
			for(var i:int = 0;i < hCellCount;i++)
			{
				for(var j:int = 0;j< vCellCount;j++)
				{
			
					var cellHeight:Number = Math.min(bpData.height - i * CELL_SIZE, CELL_SIZE);
					var cellWidth:Number = Math.min(bpData.width - j * CELL_SIZE,CELL_SIZE);
					
					if(cellHeight < 1 || cellWidth < 1)
						continue;
					
					var cellbpData:BitmapData = new BitmapData(cellWidth,cellHeight,true,0xFFFFFFFF);
					cellbpData.copyChannel(bpData,new Rectangle(CELL_SIZE * j,CELL_SIZE * i,cellWidth,cellHeight),
						new Point(0,0),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA);
					
					var noneZeroArea:Rectangle = cellbpData.getColorBoundsRect(0xFFFFFFFF,0xFFFFFFFF,true);
					if(noneZeroArea.width == 0 && noneZeroArea.height == 0)
						continue;
					
					graphics.drawRect(CELL_SIZE * j ,CELL_SIZE * i,cellWidth,cellHeight);
				}
			}
			graphics.endFill();
		}
		
		public function set desaturate(_desaturate:Boolean):void
		{
			if(_desaturate == this.desaturate)
				return;
			this._desaturate = _desaturate;
			
			for(var i:int = 0;i < allFilters.length;i++)
			{
				if(allFilters[i] == bwFilter)
				{
					allFilters.splice(i,1);
					break;
				}
			}
			
			if(desaturate)
				allFilters.push(bwFilter);

			filters = allFilters;
		}
		
		public function get desaturate():Boolean
		{
			return _desaturate;	
		}
		
		public override function set filters(value:Array):void
		{
			super.filters = value;
			allFilters = value;
		}
		
		private function changeButtonState(evt:Event):void
		{
			for(var i:int = 0;i < allFilters.length;i++)
			{
				if(allFilters[i] == brightFliter)
				{
					allFilters.splice(i,1);
					break;
				}
			}
			
			switch(evt.type)
			{
				case MouseEvent.MOUSE_DOWN:
					allFilters.push(brightFliter);
				 	filters = allFilters;
					break;
				
				case MouseEvent.MOUSE_UP:
				case MouseEvent.ROLL_OUT:	
					filters = allFilters;
					
					break;
			}
		}
		
	}
}